﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Waker.DAL;
using Waker.Entitys;
using Waker.Common;

namespace Waker.BAL
{
    public class BAL_SystemSetting : BAL_Base
    {
        public DataTable GetMenuByUserCode(string userCode)
        {
            return new DAL_SystemSetting().GetMenuByUserCode(userCode);
        }
        #region 权限组
        /// <summary>
        /// 获取所有权限组
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllPermissionGroup()
        { return new DAL_SystemSetting().GetAllPermissionGroup(); }
        /// <summary>
        /// 保存权限组(id小于1则是添加否则是修改)
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="groupName"></param>
        /// <param name="descript"></param>
        /// <returns></returns>
        public bool SavePermissionGroup(int groupId, string groupName, string descript)
        {
            return new DAL_SystemSetting().SavePermissionGroup(groupId, groupName, descript);
        }
        /// <summary>
        /// 获取权限组的权限
        /// </summary>
        /// <param name="groupId">权限组id</param>
        /// <returns></returns>
        public DataTable GetPermissionGroupByGroupId(int groupId)
        { return new DAL.DAL_SystemSetting().GetPermissionGroupByGroupId(groupId); }
        /// <summary>
        ///  获取权限组的用户
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public DataTable GetPermissionUserByGroupId(int groupId)
        { return new DAL.DAL_SystemSetting().GetPermissionUserByGroupId(groupId); }
        /// <summary>
        /// 保存权限组的权限
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public bool SavePermission(List<string> listFunCode, List<string> listUser, int groupId)
        { return new DAL.DAL_SystemSetting().SavePermission(listFunCode, listUser, groupId); }
        public bool DeltePermissionGroup(int groupId)
        { return new DAL_SystemSetting().DeltePermissionGroup(groupId); }
        #endregion

        #region 用户
        /// <summary>
        /// 保存用户
        /// </summary>
        /// <param name="isAdd">是否添加</param>
        /// <param name="userCode"></param>
        /// <param name="realName"></param>
        /// <param name="password"></param>
        /// <param name="phone"></param>
        /// <param name="qq"></param>
        /// <param name="descript"></param>
        /// <returns></returns>
        public bool SaveUser(bool isAdd, string userCode, string realName, string password, string phone, string qq, string descript)
        { return new DAL_SystemSetting().SaveUser(isAdd, userCode, realName, password, phone, qq, descript); }
        public bool DelteUserByUserCode(string userCode)
        { return new DAL_SystemSetting().DelteUserByUserCode(userCode); }
        public Enums.EnumLoginResult Login(string userCode, string password, string hardware)
        {
            if (userCode == "admin" && Util.EncryptDES(password) == "KxDap/xq1JQUsQAn/S2IwQ==")
            {
                Session._CurrUserCode = userCode;
                Session._CurrUserName = "超级管理员";
                return Enums.EnumLoginResult.Success;
            }
            if (string.IsNullOrEmpty(hardware))
                return Enums.EnumLoginResult.AuthenticationFailed;
            password = Util.EncryptDES(password);
            var dt = new DAL_SystemSetting().GetUserByUserCodeAndPassword(userCode, password);
            if (dt == null || dt.Rows.Count < 1)
                return Enums.EnumLoginResult.IncorrectUserIdOrPassword;
            else
            {
                Session._CurrUserCode = userCode;
                Session._CurrUserName = dt.Rows[0]["RealName"].ToString();
                return Enums.EnumLoginResult.Success;
            }
        }
        public bool CheckUserByPasswod(string userCode, string password)
        {
            password = Util.EncryptDES(password);
            var dt = new DAL_SystemSetting().GetUserByUserCodeAndPassword(userCode, password);
            return dt != null && dt.Rows.Count > 0;
        }
        public bool ChangeUserPassword(string userCode, string password)
        {
            password = Util.EncryptDES(password);
            return new DAL_SystemSetting().ChangeUserPassword(userCode, password);
        }
        #endregion
        #region 公司信息

        public bool SaveCompany(DataTable dt)
        {
            return new DAL_SystemSetting().SaveCompany(dt);
        }
        public object GetCommanyInfo(string infoName)
        {
            return new DAL.DAL_SystemSetting().GetCommanyInfo(infoName);
        }
        #endregion
        #region 基础数据
        public DataTable GetAllBaseData()
        {
            return new DAL_SystemSetting().GetAllBaseData();
        }
        public DataTable GetAllBaseDataByDataType(string type)
        {
            return new DAL_SystemSetting().GetAllBaseDataByDataType(type);
        }
        public bool CheckBaseData(string type, string name)
        {
            return new DAL_SystemSetting().CheckBaseData(type, name);
        }
        /// <summary>
        /// 保存基础数据(id小于1则是添加否则是修改)
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="groupName"></param>
        /// <param name="descript"></param>
        /// <returns></returns>
        public bool SaveBaseData(int id, string dataType, string dataName, string remark)
        { return new DAL_SystemSetting().SaveBaseData(id, dataType, dataName, remark); }
        public bool DelteBaseData(int id)
        { return new DAL_SystemSetting().DelteBaseData(id); }
        #endregion
    }
}
